package com.douguo.recipe.network;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Handler;
import android.os.Message;
import android.util.Xml;

import com.douguo.recipe.RecipelistActivity;
import com.douguo.recipe.object.Caipin;
import com.douguo.recipe.object.Caipu;
import com.douguo.recipe.object.Caixi;
import com.douguo.recipe.object.Kouwei;
import com.douguo.recipe.object.Nandu;
import com.douguo.recipe.object.Step;
import com.douguo.recipe.object.Tese;

public class GetRecipe {
	private static final String TAG = "GetRecipe";

	public static void getCaipu(String recipeId, final Handler handler) {
		final List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("recipe_id", recipeId));
		new Thread(new Runnable() {

			public void run() {
				DouguoConnection mConn = new DouguoConnection(
						Constant.URL_GET_COOKINFO, nvps, mParseListener,
						DouguoConnection.GET_METHOD);
				Caipu recipe = (Caipu) mConn.Connect();
				handler.sendMessage(Message.obtain(handler,
						Constant.GET_RECIPE_NETWORK, recipe));

			}

		}).start();
	}

	private static OnParseListener mParseListener = new OnParseListener() {

		public Object onParsing(InputStream is) {
			// List<Caipu> recipes = null;
			XmlPullParser parser = Xml.newPullParser();
			Caipu currentCaipu = null;
			boolean done = false;

			try {

				parser.setInput(is, null);
				int eventType = parser.getEventType();
				while (eventType != XmlPullParser.END_DOCUMENT && !done) {
					String name = null;
					switch (eventType) {
					case XmlPullParser.START_TAG:
						name = parser.getName();
						if (name.equalsIgnoreCase("recipe")) {
							currentCaipu = new Caipu();
						} else if (currentCaipu != null) {
							if (name.equalsIgnoreCase("name")) {
								currentCaipu.name = parser.nextText();
							} else if (name.equalsIgnoreCase("author")) {
								currentCaipu.author = parser.getAttributeValue(
										null, "name");
							} else if (name.equalsIgnoreCase("caixi")) {
								currentCaipu.caixi = new Caixi(
										parser.getAttributeValue(null, "id"));
							} else if (name.equalsIgnoreCase("nandu")) {
								currentCaipu.nandu = new Nandu(
										parser.getAttributeValue(null, "id"));
							} else if (name.equalsIgnoreCase("kouwei")) {
								currentCaipu.kouwei = new Kouwei(
										parser.getAttributeValue(null, "id"));
							} else if (name.equalsIgnoreCase("tese")) {
								currentCaipu.tese = new Tese(
										parser.getAttributeValue(null, "id"));
							} else if (name.equalsIgnoreCase("caipin")) {
								currentCaipu.caipin = new Caipin(
										parser.getAttributeValue(null, "id"));
							} else if (name.equalsIgnoreCase("time")) {
								currentCaipu.time = parser.getAttributeValue(
										null, "value");
							} else if (name.equalsIgnoreCase("img")) {
								currentCaipu.thumbPath = parser
										.getAttributeValue(null, "src");
								if(currentCaipu.thumbPath != null && currentCaipu.thumbPath.length() > 0) {
									currentCaipu.photoPath = currentCaipu.thumbPath.replaceFirst("80_", "300_");
								}
							} else if (name.equalsIgnoreCase("steps")) {
								currentCaipu.steps = new ArrayList<Step>();
							} else if (name.equalsIgnoreCase("step")) {

								String stepStr = parser.nextText();
								String[] steps = stepStr.split("\n");
								int i = 1;
								for (String ss : steps) {
									String singleStep = ss.replaceFirst("^\\d+、",
											"");
									singleStep = singleStep.replaceFirst("^\\d+.", "");
									if(singleStep.length() == 0) {
										continue;
									}
									Step step = new Step();
									step.caipu = currentCaipu;
									step.content = singleStep;
									// step.imgPath =
									currentCaipu.steps.add(step);
									i++;
								}

							} else if (name.equalsIgnoreCase("major")) {
								currentCaipu.major = parser.nextText();
							} else if (name.equalsIgnoreCase("minor")) {
								currentCaipu.minor = parser.nextText();
							} else if (name.equalsIgnoreCase("tips")) {
								currentCaipu.tips = parser.nextText();
							} else if (name.equalsIgnoreCase("id")) {
								currentCaipu.id = parser.nextText();
							}

						}
						break;
					case XmlPullParser.END_DOCUMENT:
						done = true;
						break;
					}
					eventType = parser.next();
				}
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return currentCaipu;
		}

	};
}
